package com.bobo.controller.back.tag;

import com.alibaba.druid.util.StringUtils;
import com.bobo.annotations.RequestBody;
import com.bobo.annotations.RequestMapping;
import com.bobo.annotations.RequestMethod;
import com.bobo.controller.base.BaseServlet;
import com.bobo.model.Category;
import com.bobo.model.Tag;
import com.bobo.service.category.CategoryServiceImpl;
import com.bobo.service.category.ICategoryService;
import com.bobo.service.tag.ITagService;
import com.bobo.service.tag.TagServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by bobo on 16-10-27.
 */
@WebServlet(name = "tag",urlPatterns = {"/admin/tag/*"})
public class TagController extends BaseServlet {
    ICategoryService categoryService = new CategoryServiceImpl();
    ITagService tagService = new TagServiceImpl();

    @RequestMapping("/add")
    public String add1(HttpServletRequest request){
        List<Category> categories = categoryService.getCategories(1);
        request.setAttribute("categories",categories);
        return "jsp/back/tag/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        int cid = Integer.parseInt(request.getParameter("cid"));
        Category category = categoryService.getById(cid);
        Tag tag = new Tag();
        tag.setName(name);
        tag.setCategory(category);
        tagService.create(tag);
        return "redirect:/admin/category/get?id="+cid;
    }

    @RequestBody
    @RequestMapping("/del")
    public String del(HttpServletRequest request, HttpServletResponse response){
        int id = Integer.parseInt(request.getParameter("id"));
        boolean delete = tagService.delete(id);
        if(delete){
            return "true";
        }else {
            return "false";
        }

    }

    @RequestMapping(value = "/upd",method = RequestMethod.POST)
    public String updd(HttpServletRequest request, HttpServletResponse response){
        int tid =  Integer.parseInt(request.getParameter("tid"));
        String name = request.getParameter("name");
        int status = Integer.parseInt(request.getParameter("status"));

        int cid = Integer.parseInt(request.getParameter("cid"));
        Category category = categoryService.getById(cid);

        Tag tag = tagService.getById(tid);
        tag.setName(name);
        tag.setCategory(category);
        tag.setStatus(status);
        tagService.update(tag);
        return "redirect:/admin/category/get?id="+cid;
    }

    @RequestMapping("/upd")
    public String upd(HttpServletRequest request){
        List<Category> categories = categoryService.getCategories(1);
        request.setAttribute("categories",categories);

        int id = Integer.parseInt(request.getParameter("id"));
        Tag tag = tagService.getById(id);
        request.setAttribute("tag",tag);
        return "jsp/back/tag/upd";
    }

    @RequestMapping("/get")
    public String get(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Tag tag = tagService.getById(id);
        request.setAttribute("tag",tag);
        return "jsp/back/tag/get";
    }

    @RequestBody
    @RequestMapping("/changeStatus")
    public String changeStatus(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Tag tag = tagService.getById(id);
        int status = Integer.parseInt(request.getParameter("status"));
        tag.setStatus(status);
        tagService.update(tag);
        return "true";
    }
}
